package com.stu.disruptor.sharingvalue;

public class FalseSharing implements Runnable{
        public final static long ITERATIONS = 500L * 1000L * 100L;
        private int arrayIndex = 0;
 
        private static ValueNoPadding[] longs;
        public FalseSharing(final int arrayIndex) {
            this.arrayIndex = arrayIndex;
        }
 
        public static void main(final String[] args) throws Exception {
//            Thread num 1 duration = 400
//            Thread num 2 duration = 453
//            Thread num 3 duration = 520
//            Thread num 4 duration = 511
//            Thread num 5 duration = 547
//            Thread num 6 duration = 596
//            Thread num 7 duration = 646
//            Thread num 8 duration = 679
//            Thread num 9 duration = 823

//            Thread num 1 duration = 405
//            Thread num 2 duration = 1682
//            Thread num 3 duration = 2032
//            Thread num 4 duration = 2143
//            Thread num 5 duration = 3433
//            Thread num 6 duration = 3826
//            Thread num 7 duration = 3679
//            Thread num 8 duration = 3751
//            Thread num 9 duration = 3801
            for(int i=1;i<10;i++){
                System.gc();
                final long start = System.currentTimeMillis();
                runTest(i);
                System.out.println("Thread num "+i+" duration = " + (System.currentTimeMillis() - start));
            }
 
        }
        // num_threads
        private static void runTest(int NUM_THREADS) throws InterruptedException {
            Thread[] threads = new Thread[NUM_THREADS];
            longs = new ValueNoPadding[NUM_THREADS];
            for (int i = 0; i < longs.length; i++) {
                longs[i] = new ValueNoPadding();
            }
            for (int i = 0; i < threads.length; i++) {
                threads[i] = new Thread(new FalseSharing(i));
            }
 
            for (Thread t : threads) {
                t.start();
            }
 
            for (Thread t : threads) {
                t.join();
            }
        }
 
        public void run() {
            long i = ITERATIONS + 1;
            while (0 != --i) {
                longs[arrayIndex].value = 0L;
            }
        }
 
        public final static class ValuePadding {
            protected long p1, p2, p3, p4, p5, p6, p7;
            protected volatile long value = 0L;
            protected long p9, p10, p11, p12, p13, p14;
            protected long p15;
        }
        public final static class ValueNoPadding {
            // protected long p1, p2, p3, p4, p5, p6, p7;
            protected volatile long value = 0L;
            // protected long p9, p10, p11, p12, p13, p14, p15;
        }
}